package org.jeecg.modules.tls.blog.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.math3.stat.inference.BinomialTest;
import org.jeecg.modules.tls.blog.article.entity.BlogArticle;
import org.jeecg.modules.tls.blog.article.service.IBlogArticleService;
import org.jeecg.modules.tls.blog.article.service.impl.BlogArticleServiceImpl;
import org.jeecg.modules.tls.blog.category.entity.BlogCategory;
import org.jeecg.modules.tls.blog.category.mapper.BlogCategoryMapper;
import org.jeecg.modules.tls.blog.category.service.IBlogCategoryService;
import org.jeecg.modules.tls.blog.category.vo.CategoryVo;
import org.jeecg.modules.tls.blog.common.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: blog_category
 * @Author: jeecg-boot
 * @Date:   2023-01-07
 * @Version: V1.0
 */
@Service
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory> implements IBlogCategoryService {

    @Autowired
    private IBlogArticleService blogArticleService;

    @Override
    public ResponseResult getCategoryList() {
        LambdaQueryWrapper<BlogCategory> categoryWrapper = new LambdaQueryWrapper<>();
        List<BlogCategory> categoryList = list(categoryWrapper);

        //bean拷贝
        List<CategoryVo> categoryVos = new ArrayList<>();
        for (BlogCategory blogCategory : categoryList) {
            CategoryVo vo = new CategoryVo();
            Integer count = 0;
            QueryWrapper<BlogArticle> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_id", blogCategory.getId());
            count = Math.toIntExact(blogArticleService.count(queryWrapper));
            vo.setTotal(count);
            BeanUtils.copyProperties(blogCategory,vo);
            categoryVos.add(vo);
        }

        return ResponseResult.okResult(categoryVos);
    }
}
